import config from "./config"
import md5 from "./md5"
// http请求类
export default class Http{
    // 封装验证
    setHeadre(){
        let time = Date.now();
        return {
            username : config.username,
            password : config.password,
            timestemp: time,
            sign : md5(config.username+config.password+config.token+time),
        }
    }
    httpRequest({ url, data = {}, method = "GET", header = {} }) {
        wx.showLoading({
            title: "正在玩命加载中...",
            mask: true,
        });
        let time = Date.now();
        header = {...this.setHeadre(),...header};
        return new Promise((resolve,reject) => {
            wx.request({
                url:config.domain+url,
                data,
                header,
                method,
                success:ret => resolve(ret),
                fail:err => reject(err),
                complete:()=>{
                    wx.hideLoading();
                }
            })
        })
    }
    // 封装图片请求函数
    uploadFile({ url,filePath,formData={node:"renting"}}){
        // 返回对象的
        return new Promise((resolve,reject)=>{
            // 发起请求
            wx.uploadFile({
                // 请求的url
                url: config.domain + url,
                // 数据
                filePath, 
                // 将用户的信息包括时间戳和加密的sign
                header:this.setHeadre(),
                name: "file",
                formData,
                success:result=>resolve(ret),
            });
        })
    };
}